import loginMix from './login-mix'
import {
  mapMutations
} from 'vuex'

export default {
  mixins: [loginMix],
  mounted () {
    this.$nextTick(() => {
      this.codeIndex = this.$cache.guid()
      this.ui.code_src = `${this.basePath}/validateCode.jpegx?index=${this.codeIndex}&timestamp=${+new Date()}`
      this.UNBIND_ALL()
    })
  },
  methods: {
    ...mapMutations([
      'BIND_USER',
      'BIND_MENUS',
      'UNBIND_ALL',
      'BIND_CONFIG',
      'BIND_DATAPERMISSION',
      'BIND_TABS',
      'BIND_URIS'
    ]),
    async readMenu (userid) {
      let resource = await this.$get('cpResource/getRes.json')
      let menus = resource
      const tabs = []
      const uriMap = []
      menus.forEach(menu => {
        menu.index = false
        if (menu.view === this.index) {
          menu.index = true
          menu.closable = false
          tabs.push({
            name: 'all-index',
            path: menu.view,
            title: menu.name,
            active: true
          })
        }
        if (menu.view) {
          uriMap.push({
            uri: menu.view,
            title: menu.name
          })
        }
        menu.show = false
        if (menu.menus && menu.menus.length > 0) {
          menu.isParent = true
          // 一级菜单下面直接访问的菜单
          menu.directlyMenus = []
          // 一级菜单下面还有子菜单的菜单
          menu.indirectMenus = []
          menu.menus.forEach(menu2 => {
            menu2.active = false
            menu2.index = false
            menu2.closable = true
            if (menu2.view) {
              uriMap.push({
                uri: menu2.view,
                title: menu2.name
              })
            }
            if (!menu2.menus || menu2.menus.length === 0) {
              menu.directlyMenus.push(menu2)
            } else {
              menu.indirectMenus.push(menu2)
              menu2.menus.forEach(menu3 => {
                menu3.active = false
                menu3.closable = true
                menu3.index = false
                if (menu3.view) {
                  uriMap.push({
                    uri: menu3.view,
                    title: menu3.name
                  })
                }
              })
            }
          })
        }
      })
      this.BIND_URIS(uriMap)
      this.BIND_TABS(tabs)
      this.BIND_MENUS(menus)
    },
    async readConfig () {
      let config = await this.$get('/GlobalValues.json')
      this.BIND_CONFIG(config)
    },
    async readPermission () {
      let permissions = await this.$get('cpResource/getPermissions.json')
      this.BIND_DATAPERMISSION(permissions)
    },
    async loginSuccess () {
      let path = this.index
      this.$router.replace({
        path
      })
      this.ui.loging = false
    },
    async validForm () {

    },
    async getUser () {

    },
    resetForm (force) {
      this.ui.loging = false
      if (force === true) {
        event.target.src = `${this.basePath}/validateCode.jpegx?index=${this.codeIndex}&timestamp=${+new Date()}`
      }
    },
    async login () {
      if ((await this.validForm()) === false) {
        this.resetForm()
      } else {
        this.ui.loging = true
        try {
          let user = await this.getUser()
          if (!user) {
            return this.resetForm(true)
          }
          this.UNBIND_ALL()
          this.BIND_USER(user)
          this.$cache.set(this.$cache.keys.devid, user.devid)
          await this.readMenu(user.userid)
          await this.readConfig()
          await this.readPermission()
          await this.loginSuccess()
        } catch (e) {
          this.resetForm(true)
        }
      }
    }
  }
}
